$(function () {
    $(".home_box").on('click','.deduction',function () {//点击划扣
        $(this).parent().siblings().children('input').css('border','1px solid #cccccc').attr('readonly',false);
        $(this).hide().siblings().show();
    });

    $(".home_box").on('click','.updated',function () {//点击修改
        $(this).parent().siblings().children('input').css('border','1px solid #cccccc').attr('readonly',false);
        $(this).siblings().show();
    });

    $(".home_box").on('click','.cancle',function () {//点击取消
        $(this).parent().siblings().children('input').css('border','none').attr('readonly',true);
        $(this).hide().siblings('.hideDeduction').hide().siblings('.deduction').show();
        $(this).parent().siblings().children('.serviceActualPay').val($(this).parent().siblings().children('.actualPay').val());
        $(this).parent().siblings().children('.deductionAmount').val($(this).parent().siblings().children('.deductionPay').val())
    });

    $(".home_box").on('click','.cancle1',function () { //点击取消
        $(this).parent().siblings().children('input').css('border','none').attr('readonly',true);
        $(this).hide().siblings('.hideDeduction1').hide().siblings('.deduction').show();
        $(this).parent().siblings().children('.serviceActualPay').val($(this).parent().siblings().children('.actualPay').val());
        $(this).parent().siblings().children('.deductionAmount').val($(this).parent().siblings().children('.deductionPay').val())
    });

    $(".home_box").on('click','.cancle1',function () {//划价记录点击取消
        $(this).parent().siblings().children('input').css('border','none').attr('readonly',true);
        $(this).parent().siblings().children('.updateServiceActualPay').val($(this).parent().siblings().children('.updateActualPay').val());
        $(this).parent().siblings().children('.updateDeduction').val($(this).parent().siblings().children('.updateDeduction').val())
        $(this).hide().siblings('.sure').hide();
    });

    $(".home_box").on('click','.hideDeduction',function () {//点击确认
        $(this).hide();
        $(this).parent().siblings().children('input').css('border','none').attr('readonly',true);
        var payServiceName=$(this).parent().siblings('.payServiceName').html();
        var accountArrears=$(this).parent().siblings('.accountArrears').html();
        var payServiceNum=$(this).parent().siblings('.payServiceNum').html();
        var serviceActualPay=$(this).parent().siblings().children('.serviceActualPay').val();
        var actualPay=$(this).parent().siblings().children('.actualPay').val();
        var deductionPay=$(this).parent().siblings().children('.deductionPay').val();
        var deductionAmount=$(this).parent().siblings().children('.deductionAmount').val();
        var remarks=$(this).parent().siblings().children('.remarks').val();
        var id2 = $(this).parents('.payServiceBox').attr('id');
        var customerId =$("#customerId").val();
        if(parseInt(serviceActualPay) > parseInt(actualPay)){
             alert("实收金额不能大于实际金额,请重新输入")
            return ;
        }
        if(parseInt(deductionAmount) > parseInt(deductionPay)){
            alert("抵扣金额不能大于实际抵扣,请重新输入")
            return ;
        }

        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/insert_price",
            type: "post",
            data: {
                payServiceName: payServiceName,
                accountArrears: accountArrears,
                serviceActualPay: serviceActualPay,
                payServiceNum:payServiceNum,
                deductionAmount: deductionAmount,
                remarks: remarks,
                customerId:customerId,
                id2: id2
            },
            success: function (data) {
                Feng.success('操作成功!');
                window.parent.DoctorLooked.search();
                window.location.reload();
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    });

    $(".home_box").on('click','.hideDeduction1',function () {//点击确认
        $(this).hide();
        $(this).parent().siblings().children('input').css('border','none').attr('readonly',true);
        var payServiceName=$(this).parent().siblings('.payServiceName').html();
        var accountArrears=$(this).parent().siblings('.accountArrears').html();
        var payServiceNum=$(this).parent().siblings('.payServiceNum').html();
        var serviceActualPay=$(this).parent().siblings().children('.serviceActualPay').val();
        var actualPay=$(this).parent().siblings().children('.actualPay').val();
        var deductionPay=$(this).parent().siblings().children('.deductionPay').val();
        var deductionAmount=$(this).parent().siblings().children('.deductionAmount').val();
        var remarks=$(this).parent().siblings().children('.remarks').val();
        var id2 = $(this).parents('.payServiceBox').attr('id');
        var customerId =$("#customerId").val();
        if(parseInt(serviceActualPay) > parseInt(actualPay)){
            alert("实收金额不能大于实际金额,请重新输入")
            return ;
        }
        if(parseInt(deductionAmount) > parseInt(deductionPay)){
            alert("抵扣金额不能大于实际抵扣,请重新输入")
            return ;
        }

        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/insert_price",
            type: "post",
            data: {
                payServiceName: payServiceName,
                accountArrears: accountArrears,
                serviceActualPay: serviceActualPay,
                payServiceNum:payServiceNum,
                deductionAmount: deductionAmount,
                remarks: remarks,
                customerId:customerId,
                id2: id2
            },
            success: function (data) {
                Feng.success('操作成功!');
                window.location.href= Feng.ctxPath + '/doctorDetails/doctor_seeDiagnosis/' + customerId+'/'+6;
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    });

    $(".home_box").on('click','.sure',function () {//修改划价确认
        $(this).hide();
        $(this).parent().siblings().children('input').css('border','none').attr('readonly',true);
        var serviceActualPay=$(this).parent().siblings().children('.updateServiceActualPay').val();
        var deductionAmount=$(this).parent().siblings().children('.updateDeduction').val();
        var createTime=$(this).parent().siblings('.createTime').html();
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/update_price",
            type: "post",
            data: {
                serviceActualPay: serviceActualPay,
                deductionAmount: deductionAmount,
                createTime: createTime
            },
            success: function (data) {
                Feng.success('操作成功!');
                window.location.reload();
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    });

    $(".home_box").on('click','.deleted',function () {//删除
        $(this).hide();
        $(this).parent().siblings().children('input').css('border','none').attr('readonly',true);
        var createTime=$(this).parent().siblings('.createTime').html();
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/delete_price",
            type: "post",
            data: {
                createTime: createTime
            },
            success: function (data) {
                Feng.success('操作成功!');
                window.location.reload();
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    });

    serviceActualPayNetReceipts = function (v) {
        var value1 = $(v).val();
        var value2 = $(v).parent().siblings().children('.netDeduction').val();
        var actualPay=$(v).siblings('.actualPay').val();
        if(parseFloat(value1) > parseFloat(actualPay)){
            layer.msg("不能大于已划扣金额,请重新输入")
            return false;
        }
        if(value1 == "" || value1 == undefined || value1 == null){ value1 = 0}
        if(value2 == "" || value2 == undefined || value2 == null){ value2 = 0}
        var num = parseFloat(value1) +  parseFloat(value2);
        $(v).parent().siblings('.subtotal').html(num);
    };

    deductionAmountNetReceipts = function (v) {
        var value1 = $(v).val();
        var value2 = $(v).parent().siblings().children('.netDeduction').val();
        var deductionPay=$(v).siblings('.deductionPay').val();
        if(parseFloat(value1) > parseFloat(deductionPay)){
            layer.msg("不能大于积分可划扣金额,请重新输入")
            return false;
        }

        if(value1 == "" || value1 == undefined || value1 == null){ value1 = 0}
        if(value2 == "" || value2 == undefined || value2 == null){ value2 = 0}
        var num = parseFloat(value1) +  parseFloat(value2);
        $(v).parent().siblings('.subtotal').html(num);
    };

    updateServiceActualPay = function (v) {//划价记录判断 实收
        var value1 = $(v).val();
        var value2 = $(v).parent().siblings().children('.updateNetDeduction').val();
        var updateActualPay=$(v).siblings('.updateActualPay').val();
        if(parseFloat(value1) > parseFloat(updateActualPay)){
            layer.msg("不能大于实收可划扣金额,请重新输入")
            return false;
        }
        if(value1 == "" || value1 == undefined || value1 == null){ value1 = 0}
        if(value2 == "" || value2 == undefined || value2 == null){ value2 = 0}
        var num = parseFloat(value1) +  parseFloat(value2);
        $(v).parent().siblings('.subtotall').html(num);
    };

    updateDeduction = function (v) {//划价记录判断 积分
        var value1 = $(v).val();
        var value2 = $(v).parent().siblings().children('.updateNetDeduction').val();
        var updateDeduction=$(v).siblings('.updateDeduction').val();
        if(parseFloat(value1) > parseFloat(updateDeduction)){
            layer.msg("不能大于实收可划扣金额,请重新输入")
            return false;
        }
        if(value1 == "" || value1 == undefined || value1 == null){ value1 = 0}
        if(value2 == "" || value2 == undefined || value2 == null){ value2 = 0}
        var num = parseFloat(value1) +  parseFloat(value2);
        $(v).parent().siblings('.subtotall').html(num);
    };

    netReceipts = function (v) {
        var value1 = $(v).val();
        var value2 = $(v).parent().siblings().children('.netDeduction').val();
        if(value1 == "" || value1 == undefined || value1 == null){ value1 = 0}
        if(value2 == "" || value2 == undefined || value2 == null){ value2 = 0}
        var num = parseFloat(value1) +  parseFloat(value2);
        $(v).parent().siblings('.subtotal').html(num);
    };

    $(".showDetailed").click(function () {
        var id1 = $(this).parent().siblings().attr('id');
        var id2 = $(this).parent().attr('id');
        var content = "";
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/getPriceInfo",
            type: "post",
            data: {
                id2:id2
            },
            success:function (arr) {
                if(arr.length > 0){
                    var num = 0;
                    var nums = 0;
                    var serviceActualPay=0;
                    var deduction =0;
                    content += "<div style='box-shadow: 0 0 8px #cccccc;border-radius: 6px;overflow: hidden;'><div style='display: flex;background: #fec182;text-align: center;padding: 4px 0;'><div style='flex: 1'>划扣时间</div><div style='flex: 1'>实收</div><div style='flex: 1'>抵扣</div><div style='flex: 1'>小计</div><div style='flex: 1'>医生</div><div style='flex: 1'>操作</div></div>";
                    for(var i=0 ; i<arr.length; i++){
                        num = parseFloat(arr[i].serviceActualPay) + parseFloat(arr[i].deduction);
                        serviceActualPay += parseFloat(arr[i].serviceActualPay);
                        deduction += parseFloat(arr[i].deduction);
                        nums += num;
                        content += "<div style='display: flex;border-top: 1px solid #eeeeee;text-align: center;padding: 7px 0;'>" +
                            "<div style='flex: 1;' class='createTime'>"+arr[i].createTime+"</div>" +
                            "<div style='flex: 1;'><input type='hidden' class='updateActualPay' value='"+arr[i].serviceActualPay+"'><input type='number' value='"+arr[i].serviceActualPay+"' align='center' readonly='readonly' style='border: none;width: 60%;text-align: center;padding-left: 15px;' oninput='updateServiceActualPay(this)' class='updateNetDeduction updateServiceActualPay'></div>" +
                            "<div style='flex: 1;'><input type='hidden' class='updateDeduction' value='"+arr[i].deduction+"'><input type='number' value='"+arr[i].deduction+"' align='center' readonly='readonly' style='border: none;width: 60%;text-align: center;padding-left: 15px;' oninput='updateDeduction(this)' class='updateNetDeduction updateDeduction'></div>" +
                            "<div style='flex: 1;' class='subtotall'>"+num+"</div>" +
                            "<div style='flex: 1;'>"+arr[i].doctor+"</div>" +
                            "<span style='flex: 1;'><a href='javascript:;' style='color: #da6792;margin-right: 10px;' class='deleted'>删除</a><a href='javascript:;' style='color: #da6792;margin-right: 10px;' class='updated' >修改</a><a href='javascript:;' style='color: #da6792;margin-right: 10px;display: none'class='sure'>确定</a><a href='javascript:;' style='color: #da6792;display: none' class='cancle1'>取消</a></span>" +
                            "</div>"
                    }
                    content += "<div style='display: flex;font-weight: bold;padding-bottom: 7px;'><div style='flex: 1;'>合计</div><div style='flex: 1'>"+serviceActualPay+"</div><div style='flex: 1'>"+deduction+"</div><div style='flex: 1'>"+nums+"</div><div style='flex: 1;'></div><div style='flex: 1;'></div></div>"
                    content += "</div>";
                    $("#"+id1).html(content).toggle();
                }
            }
        });
        $(this).children().toggle();
    });
});